package user

import (
	"app-api/app/internal/svc"
	"app-api/app/internal/types"
	"app-api/protos/user"
	"context"
	"encoding/json"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo() (resp *types.UserInfo, err error) {
	userID, _ := l.ctx.Value("id").(json.Number).Int64()
	user, err := l.svcCtx.UserRPC.GetUser(l.ctx, &user.IDRequest{Id: userID})
	if err != nil {
		return nil, err
	}
	return &types.UserInfo{Id: user.Id, Name: user.Name}, nil
}
